package com.agileboot.admin.controller.airport;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.airport.inspection.SirInspectionApplicationService;
import com.agileboot.domain.airport.inspection.command.AddSirInspectionCommand;
import com.agileboot.domain.airport.inspection.command.UpdateSirInspectionCommand;
import com.agileboot.domain.airport.inspection.dto.SirInspectionDTO;
import com.agileboot.domain.airport.inspection.query.SirInspectionQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 检查计划信息操作处理
 *
 * @author valarchie
 */
@Tag(name = "检查计划API", description = "检查计划相关的增删查改")
@RestController
@RequestMapping("/safety/inspection")
@Validated
@RequiredArgsConstructor
public class SirInspectionController extends BaseController {

    private final SirInspectionApplicationService inspectionApplicationService;

    /**
     * 获取检查计划列表
     */
    @Operation(summary = "检查计划列表")
//    @PreAuthorize("@permission.has('safety:inspection:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<SirInspectionDTO>> list(SirInspectionQuery query) {
        PageDTO<SirInspectionDTO> pageDTO = inspectionApplicationService.getInspectionList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 导出查询到的所有检查计划信息到excel文件
     * @param response http响应
     * @param query 查询参数
     * @author Kevin Zhang
     * @date 2025-07-10
     */
    @Operation(summary = "检查计划列表导出")
    @AccessLog(title = "检查计划管理", businessType = BusinessTypeEnum.EXPORT)
//    @PreAuthorize("@permission.has('safety:inspection:export')")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, SirInspectionQuery query) {
        List<SirInspectionDTO> all = inspectionApplicationService.getInspectionListAll(query);
        CustomExcelUtil.writeToResponse(all, SirInspectionDTO.class, response);
    }

    /**
     * 根据检查计划编号获取详细信息
     */
    @Operation(summary = "检查计划详情")
//    @PreAuthorize("@permission.has('safety:inspection:query')")
    @GetMapping(value = "/{inspectionId}")
    public ResponseDTO<SirInspectionDTO> getInfo(@PathVariable Long inspectionId) {
        SirInspectionDTO inspection = inspectionApplicationService.getInspectionInfo(inspectionId);
        return ResponseDTO.ok(inspection);
    }

    /**
     * 新增检查计划
     */
    @Operation(summary = "添加检查计划")
//    @PreAuthorize("@permission.has('safety:inspection:add')")
    @AccessLog(title = "检查计划管理", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddSirInspectionCommand addCommand) {
        addCommand.generateInspectionCode();
        inspectionApplicationService.addInspection(addCommand);
        return ResponseDTO.ok();
    }

    /**
     * 修改检查计划
     */
    @Operation(summary = "修改检查计划")
//    @PreAuthorize("@permission.has('safety:inspection:edit')")
    @AccessLog(title = "检查计划管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateSirInspectionCommand updateCommand) {
        inspectionApplicationService.updateInspection(updateCommand);
        return ResponseDTO.ok();
    }

    /**
     * 删除检查计划
     */
    @Operation(summary = "删除检查计划")
//    @PreAuthorize("@permission.has('safety:inspection:remove')")
    @AccessLog(title = "检查计划管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        inspectionApplicationService.deleteInspection(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}